﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using BLL;
using Model;
using CrystalDecisions.CrystalReports.Engine;

public partial class ViewbyProduct_FailCategorybyTimePID : System.Web.UI.Page
{
    private DateTime dt1, dt2;

    protected void Page_Load(object sender, EventArgs e)
    {       
        if (!IsPostBack)
        {
            BindCustoms();
            BindProductTypes();
            this.CrystalReportViewer1.Visible = false;
            this.CrystalReportViewer2.Visible = false;
        }
        this.Page.Unload += new EventHandler(Page_Unload);
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
        this.CrystalReportSource2.Dispose();
    }

    protected void BindCustoms()
    {
        Customer cust = new Customer();
        this.DropDownList1.DataSource = cust.LoadEntities();
        this.DropDownList1.DataTextField = "CustomerName";
        this.DropDownList1.DataValueField = "CustomerID";
        this.DropDownList1.DataBind();
    }

    protected void BindProductTypes()
    {
        ProductType pt = new ProductType();
        this.DropDownList2.DataSource = pt.GetProductTypesbyCustomerID(this.DropDownList1.SelectedValue);
        this.DropDownList2.DataTextField = "ProductTypeName";
        this.DropDownList2.DataValueField = "ProductTypeID";
        this.DropDownList2.DataBind();
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindProductTypes();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        dt1 = DateTime.Parse(this.TextBox1.Value.Trim());
        dt2 = DateTime.Parse(this.TextBox2.Value.Trim());
        ForCrystalReport();
        ForCrystalReport1();
        this.CrystalReportViewer1.Visible = true;
        this.CrystalReportViewer2.Visible = true;
        //BindTests();
        //BindTestItems();
    }

    private void BindTests()
    {        
        string var_productTypeID = this.DropDownList2.SelectedValue;

        Test test = new Test();
        GridView1.DataSource = test.GetFailTestsbyPID_DT(var_productTypeID, dt1, dt2);
        GridView1.DataBind();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (GridView1.Rows[i].Cells[7].Text == "Fail")
                GridView1.Rows[i].Cells[7].ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void BindTestItems()
    {
        string var_productTypeID = this.DropDownList2.SelectedValue;

        TestItem testItem = new TestItem();
        this.GridView2.DataSource = testItem.GetFailTestItems(var_productTypeID, dt1, dt2);
        this.GridView2.DataBind();
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            if (GridView2.Rows[i].Cells[11].Text == "Fail")
                GridView2.Rows[i].Cells[11].ForeColor = System.Drawing.Color.Red;
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindTests();
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["productSN"] = GridView1.SelectedDataKey["ProductSN"].ToString();
        Session["testTime"] = Convert.ToDateTime(GridView1.SelectedDataKey["TestTime"]);
        Session["ProductTypeID"] = GridView1.SelectedDataKey["ProductTypeID"].ToString();
        Session["UserID"] = GridView1.SelectedDataKey["UserID"].ToString();
        Session["ENAID"] = GridView1.SelectedDataKey["ENAID"].ToString();
        Session["IsPass"] = GridView1.SelectedDataKey["IsPass"].ToString();

        Response.Redirect("~/ViewTest/ViewYourself/ViewTestItem.aspx");
    }

    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView2.PageIndex = e.NewPageIndex;
        BindTestItems();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Test test = new Test();
        TestItem testItem = new TestItem();
        dt1 = DateTime.Parse(this.TextBox1.Value.Trim());
        dt2 = DateTime.Parse(this.TextBox2.Value.Trim());
        string var_productTypeID = this.DropDownList1.SelectedItem.Text.Trim();
        List<TestInfo> testinfo = (List<TestInfo>)test.GetFailTestsbyPID_DT(var_productTypeID, dt1, dt2);
        List<TestItemInfo> testiteminfos = (List<TestItemInfo>)testItem.GetFailTestItems(var_productTypeID, dt1, dt2);
   

        if (!Directory.Exists(Server.MapPath(".") + "\\测试数据"))
        {
            DirectoryInfo d = Directory.CreateDirectory(Server.MapPath(".") + "\\测试数据");
        }
        string filename = Server.MapPath(".") + "\\测试数据\\" + var_productTypeID + " " + dt1.ToString("yyyyMMddHHmm") + "-" + dt2.ToString("yyyyMMddHHmm")+".csv";//.Year + testinfo.TestTime.Month + testinfo.TestTime.Day + " " + testinfo.TestTime.Hour + testinfo.TestTime.Minute + ".txt";
        using (StreamWriter sw = File.CreateText(filename))
        {
            sw.WriteLine("ProductSN,TestTime,ProductTypeID,UserID,ENAID,IsSwitch,IsPass");
            foreach (TestInfo ti in testinfo)
            {
                sw.WriteLine(ti.ProductSN + ",    " + ti.TestTime + ",  " + ti.ProductTypeID + ",   " + ti.UserID + ",   " + ti.ENAID + ",    " + ti.IsSwitch + ",  " + ti.IsPass);
            }
            sw.WriteLine();
            sw.WriteLine();
            sw.WriteLine("TestItemName,ProductSN,TestTime,TestTypeName,StartF,StopF,Max,Min,HighSpec,LowSpec,IsPass");
            foreach (TestItemInfo tii in testiteminfos)
            {
                sw.WriteLine(tii.TestItemName + ",  " + tii.ProductSN + ",  " + tii.TestTime + ",   " + tii.TestTypeName + ", " + tii.StartF + ", " + tii.StopF + ",  " + tii.Max + ",    " + tii.Min + ",    " + tii.HighSpec + ",   " + tii.LowSpec + ",    " + tii.IsPass);
            }
            sw.Close();
        }
        //}
        this.DownLoadFile(filename);
    }

    private void DownLoadFile(string fileName)
    {
        string filePath = fileName;
        if (File.Exists(filePath))
        {
            FileInfo file = new FileInfo(filePath);
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //解决中文乱码
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解决中文文件名乱码    
            Response.AddHeader("Content-length", file.Length.ToString());
            Response.ContentType = "appliction/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();
        }
    }

    protected void ForCrystalReport()
    {
        //doc.Load(Server.MapPath("ViewFailTests.rpt"));
        //IList<TestInfo> list = GetTestData();
        //doc.SetDataSource(list);
        //this.CrystalReportViewer1.ReportSource = doc;
        //this.CrystalReportViewer1.DataBind();

        //object t = new object();                                    //Object类型可行
        //ArrayList lst = new ArrayList();
        //DataSet ds = new DataSet();

        CrystalReportSource1.ReportDocument.Load(Server.MapPath("ViewFailTests.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text1"];
        txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<TestInfo> list = GetTestData();   
        CrystalReportSource1.ReportDocument.SetDataSource(list);
        CrystalReportSource1.DataBind();
        this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind();
    }

    public IList<TestInfo> GetTestData()
    {
        IList<TestInfo> tests = new List<TestInfo>();
        Test test = new Test();
        string var_productTypeID = this.DropDownList2.SelectedValue;
        tests = test.GetFailTestsbyPID_DT(var_productTypeID, dt1, dt2);
        return tests;
    }

    protected void ForCrystalReport1()
    {
        //doc1.Load(Server.MapPath("ViewFailTestItems.rpt"));
        //IList<TestItemInfo> list = GetTestItemData();
        //doc1.SetDataSource(list);        
        //this.CrystalReportViewer2.ReportSource = doc1;
        //this.CrystalReportViewer2.DataBind();

        //使用CrystalReportSource方案的版本CrystalReportSource、CrystalReportViewer都是.NET封装的控件类
        CrystalReportSource2.ReportDocument.Load(Server.MapPath("ViewFailTestItems.rpt"));

        TextObject txtTestTime = (TextObject)this.CrystalReportSource2.ReportDocument.ReportDefinition.ReportObjects["Text12"];
        txtTestTime.Text = dt1.ToString() + "-" + dt2.ToString();

        IList<TestItemInfo> list = GetTestItemData();
        CrystalReportSource2.ReportDocument.SetDataSource(list);
        CrystalReportSource2.DataBind();
        this.CrystalReportViewer2.ReportSource = CrystalReportSource2;
        CrystalReportViewer2.DataBind();
    }

    public IList<TestItemInfo> GetTestItemData()
    {
        IList<TestItemInfo> testItems = new List<TestItemInfo>();
        TestItem testItem = new TestItem();
        string var_productTypeID = this.DropDownList2.SelectedValue;
        testItems = testItem.GetFailTestItems(var_productTypeID, dt1, dt2);
        return testItems;
    }
}
